package xfsale.service.impl.j2ee;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import xfsale.exception.AppException;
import xfsale.packet.TAAcctRegisterRequest;
import xfsale.packet.TAAcctRegisterResponse;

public final class TAAcctRegister extends BaseOrderAcct {

	private TAAcctRegisterRequest request;
	private TAAcctRegisterResponse response;

	public TAAcctRegister(TAAcctRegisterRequest request, TAAcctRegisterResponse response) {
		super(request, response);
		this.request = request;
		this.response = response;
		response.returnCode = "0000";
		response.returnMsg = "基金账户登记成功";
	}

	//检查输入参数
    @Override
	protected void checkData() throws Exception {
		super.checkData();

		checkTACode(request.taCode);

		checkTAAccountID(request.taAccountID);
	}

	//初始化
    @Override
	protected void load() throws Exception {
		super.load();
	}

	//检查功能权限
    @Override
	protected void checkPriv() throws Exception {
		super.checkPriv();
	}

	//检查条件是否满足
    @Override
	protected void checkCondition() throws Exception {
		super.checkCondition();

		PreparedStatement stmt = null;
		try {
			//检查基金帐号是否已存在
			logger.debug("检查基金帐号是否已存在...");
			stmt = conn.prepareStatement("" +
				"select count(*) " +
				"from AcctTA " +
				"where taCode = ? " +
				"and taAccountID = ? "
			);
			stmt.setString(1, request.taCode);
			stmt.setString(2, request.taAccountID);
			ResultSet rs = stmt.executeQuery();
			rs.next();
			int count = rs.getInt(1);
			if (count > 0) {
				throw new AppException("基金账户已存在");
			}
		} catch (Exception e) {
			logger.error("检查基金帐号出错. " + e);
			throw e;
		} finally {
			stmt.close();
		}
	}

	//功能处理
    @Override
	protected void process() throws Exception {
		//写入基金账户数据
		writeAcctTA();

		//写入账户委托数据
		writeOrderAcct();
	}

	//写入基金账户数据
	private void writeAcctTA() throws Exception {
		PreparedStatement stmt = null;
		try {
			logger.debug("写入基金账户数据...");
			stmt = conn.prepareStatement("" +
				"insert into AcctTA" +
				"(" +
				"	transAccountID, " +			//交易帐号
				"	taAccountID, " +			//基金帐号
				"	taCode, " +					//TA代码
				"	freezingDate, " +			//冻结截至日期
				"	frozenCause, " +			//冻结原因
				"	taAcctStatus, " +			//基金账户状态
				"	taAcctOpenDate, " +			//基金账户开户确认日期
				"	taAcctOpenDate, " +			//基金账户销户确认日期
				"	taAcctFreezeDate, " +		//基金账户冻结确认日期
				"	taAcctUnfreezeDate, " +		//基金账户解冻确认日期
				"	summary" +					//备注
				")" +
				"values " +
				"( " +
				"	?, " +
				"	?, " +
				"	?, " +
				"	?, " +
				"	?, " +
				"	?, " +
				"	?, " +
				"	?, " +
				"	?, " +
				"	?, " +
				"	? " +
				")"
			);
			stmt.setString(1, request.transAccountID);	//交易帐号
			stmt.setString(2, request.taAccountID);		//基金帐号
			stmt.setString(3, request.taCode);			//TA代码
			stmt.setString(4, null);					//冻结截至日期
			stmt.setString(5, null);					//冻结原因
			stmt.setString(6, "3");						//基金账户状态
			stmt.setString(7, null);					//基金账户开户确认日期
			stmt.setString(8, null);					//基金账户销户确认日期
			stmt.setString(9, null);					//基金账户冻结确认日期
			stmt.setString(10, null);					//基金账户解冻确认日期
			stmt.setString(11, null);					//备注
			stmt.executeUpdate();
			logger.debug("写入基金账户数据.");
		} catch (Exception e) {
			logger.error("写入基金账户数据出错. " + e);
			throw e;
		} finally {
			stmt.close();
		}
	}


	//写入账户委托数据
	private void writeOrderAcct() throws Exception {
		PreparedStatement stmt = null;
		ResultSet rs = null;
		ResultSet rs2 = null;
		try {
			//获取交易账户数据
			logger.debug("获取交易账户数据...");
			stmt = conn.prepareStatement("" +
				"select * " +
				"from AcctTrans " +
				"where transAccountID = ?"
			);
			stmt.setString(1, request.transAccountID);
			rs = stmt.executeQuery();
			rs.next();
			stmt.close();

			//获取客户账户数据
			logger.debug("获取客户账户数据...");
			stmt = conn.prepareStatement("" +
				"select * " +
				"from AcctCust " +
				"where custAccountID = ?"
			);
			stmt.setString(1, custAccountID);
			rs2 = stmt.executeQuery();
			rs2.next();
			stmt.close();

			//生成委托流水
			logger.debug("生成委托流水...");
			serial.setTradeDate(sys.getWorkDay());
			serial.genOrderSerial();
			response.orderSerialNo = serial.getOrderSerialNo();

			//写入账户委托数据
			logger.debug("写入账户委托数据...");
			stmt = conn.prepareStatement(SQL_WRITE_ORDER_ACCT);
            stmt.setString(1, response.orderSerialNo);		//委托流水
			stmt.setString(2, request.serviceCode);			//功能代码
            stmt.setString(3, businessCode);                //业务码
			stmt.setString(4, request.acceptMethod);		//受理方式
			stmt.setString(5, request.regionCode);			//地区编号
			stmt.setString(6, request.branchCode);			//网点编号
            stmt.setString(7, request.netCode);             //销售网点编号
			stmt.setString(8, request.opCode);				//柜员编号
			stmt.setString(9, custAccountID);               //客户帐号
			stmt.setString(10, request.transAccountID);     //交易帐号
			stmt.setString(11, request.taAccountID);		//基金帐号
			stmt.setString(12, request.taCode);				//TA代码
			stmt.setString(13, custType);                   //客户类型
			stmt.setString(14, custName);                   //客户名称
			stmt.setString(15, custNameAbbr);               //客户简称
			stmt.setString(16, nationality);                //国籍
			stmt.setString(17, certType);                   //证件类型
			stmt.setString(18, certNo);                     //证件号码
			stmt.setString(19, certValidFrom);              //证件有效期起始日期
			stmt.setString(20, certValidTo);                //证件有效期截至日期
			stmt.setString(21, rs2.getString("shSecAccountID"));            //上交所证券帐号
			stmt.setString(22, rs2.getString("szSecAccountID"));            //深交所证券帐号
			stmt.setString(23, rs2.getString("address"));                   //通讯地址
			stmt.setString(24, rs2.getString("postCode"));                  //邮政编码
			stmt.setString(25, rs2.getString("telNo"));                     //电话号码
			stmt.setString(26, rs2.getString("homeTelNo"));                 //住址电话
			stmt.setString(27, rs2.getString("officeTelNo"));               //办公电话
			stmt.setString(28, rs2.getString("mobileTelNo"));               //手机号码
			stmt.setString(29, rs2.getString("faxNo"));                     //传真号码
			stmt.setString(30, rs2.getString("email"));                     //E-MAIL地址
			stmt.setString(31, rs2.getString("birthday"));                  //出生日期
			stmt.setString(32, rs2.getString("sex"));                       //性别
			stmt.setString(33, rs2.getString("education"));                 //教育程度
			stmt.setString(34, rs2.getString("vocation"));                  //职业代码
            stmt.setString(35, rs2.getString("industry"));                  //行业代码
            stmt.setString(36, rs2.getString("marriage"));                  //婚姻状况
			stmt.setString(37, rs2.getString("income"));                    //年收入
			stmt.setString(38, rs2.getString("instType"));                  //机构类型
            stmt.setString(39, rs2.getString("instNature"));                //机构性质
            stmt.setString(40, rs2.getString("instRegAddress"));            //机构注册地
            stmt.setString(41, rs2.getString("instRegCapital"));            //机构注册资本
            stmt.setString(42, rs2.getString("instEmployees"));             //机构员工数量
			stmt.setString(43, rs2.getString("instReprName"));              //法人姓名
			stmt.setString(45, rs2.getString("instReprCertType"));          //法人证件类型
			stmt.setString(46, rs2.getString("instReprCertNo"));            //法人证件号码
			stmt.setString(47, rs2.getString("instReprCertValidFrom"));     //法人证件有效期起始日期
			stmt.setString(48, rs2.getString("instReprCertValidTo"));       //法人证件有效期截至日期
			stmt.setString(49, request.instTransName);				//经办人姓名
			stmt.setString(50, request.instTransCertType);			//经办人证件类型
			stmt.setString(51, request.instTransCertNo);			//经办人证件号码
			stmt.setString(52, request.instTransCertValidFrom);		//经办人证件有效期起始日期
			stmt.setString(53, request.instTransCertValidTo);		//经办人证件有效期截至日期
			stmt.setString(54, null);                               //经办人联系电话
			stmt.setString(55, null);                               //经办人移动电话
            stmt.setString(55, null);                               //经纪人
			stmt.setString(56, null);                       //银行代码
			stmt.setString(57, null);                       //银行全称
			stmt.setString(58, null);                       //银行帐号户名
			stmt.setString(59, null);                       //银行帐号卡号
            stmt.setString(60, null);                       //银行账号有效期
			stmt.setString(61, null);                       //银行帐号验证
			stmt.setString(62, rs.getString("deliverType"));//对帐单寄送选择
			stmt.setString(63, rs.getString("deliverWay")); //对账单寄送方式
			stmt.setString(64, null);                       //交易方式
			stmt.setString(65, null);                       //风险承受能力
			stmt.setString(66, null);						//冻结截至日期
			stmt.setString(67, null);						//冻结原因
			stmt.setString(68, null);						//原委托流水
			stmt.setString(69, null);						//原申请流水
			stmt.setString(70, "0");						//委托预约标志
			stmt.setString(71, null);						//委托预约日期
			stmt.setString(72, "0");						//是否需要授权
			stmt.setString(73, null);						//授权标志(0-未授权 1-授权通过 2-授权不通过)
			stmt.setString(74, null);						//授权操作员
			stmt.setString(75, null);						//授权日期
			stmt.setString(76, null);						//授权时间
			stmt.setString(77, "1");						//委托状态
			stmt.setString(78, sys.getWorkDay());			//委托日期
			stmt.setString(79, sys.getWorkTime());			//委托时间
			stmt.setString(80, null);                       //备注
			stmt.executeUpdate();
			logger.debug("写入账户委托数据成功.");
		} catch (Exception e) {
			logger.error("写入账户委托数据出错. " + e);
			throw e;
		} finally {
			stmt.close();
		}
	}

}